T005 自定义控件 可配置加载 ImageView

原理很简单,使一张图围绕自己的中心点匀速旋转。完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.xxt.xtest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
public class LoadingImageView extends AppCompatImageView {
public LoadingImageView(Context context) {
super(context);
init();
}
public LoadingImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public LoadingImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
RotateAnimation rotateAnim = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setRepeatCount(Animation.INFINITE);
rotateAnim.setDuration(1000);
rotateAnim.setInterpolator(new LinearInterpolator());
this.startAnimation(rotateAnim);
}
}

使用方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.xxt.xtest.LoadingImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="50dp"
android:src="@drawable/taiji"/>
</LinearLayout>